//var app = angular.module('app', []);
app.controller('DPCtr', function($scope, $http, $filter,  $state,$rootScope,filterFilter) {
	$http.get("/DRMS/api/DrmsDemandRequestPriority/getAllDrmsDemandRequestPriority").
				then(function(response) {
										// $scope.DRs = response.data;
										$scope.data = response.data;
										
										
										var priorities = [];
										
										
										for(var i=0;i<$scope.data.length;i++){
										
											
											if($scope.data[i].priority !== 'Low'){
												
												
												priorities.push($scope.data[i]);
												//console.log($scope.data[i]);
											}
										}
										
										$scope.data = priorities;
										$scope
												.$watch(
														'DPSearch+start+end',
														function(newVal, oldVal){
															var DRsFilter = [];
															
															$scope.DRs = filterFilter(
																	$scope.data,
																	$scope.DPSearch);
															if($scope.start!== undefined&&$scope.end!== undefined ){
																//console.log($scope.start);
																for(var i=0; i<$scope.DRs.length; i++){
																	
																	if( $scope.start.getTime() <= $scope.DRs[i].weekStart && $scope.end.getTime() >= $scope.DRs[i].weekEnd){
																		
																		console.log($scope.DRs[i]);
																		
																		DRsFilter.push($scope.DRs[i]);
																	}
																}
																$scope.DRs = DRsFilter;
																$scope.DRs = filterFilter(
																		$scope.DRs,
																		$scope.DPSearch);
															}
															
												/*			if($scope.end!== undefined){
																console.log($scope.end);
																for(var i=0; i<$scope.DRs.length; i++){
																	console.log($scope.DRs[i].weekEnd);
																	console.log($scope.end.getTime()+"xxxxxxxxxxxx");
																	if( $scope.end.getTime() >= $scope.DRs[i].weekEnd){
																		
																		DRsFilter.push($scope.DRs[i]);
																	}
																}
																$scope.DRs = DRsFilter;
																$scope.DRs = filterFilter(
																		$scope.DRs,
																		$scope.DPSearch);
															}*/
															
															
															
												/*			console.log();
															
															console.log($scope.start+"xxxxxxxxx");*/
															
															
															
															$scope.pageSize = 15;
															$scope.pages = Math
																	.ceil($scope.DRs.length
																			/ $scope.pageSize);
															$scope.newPages = $scope.pages > 5 ? 5
																	: $scope.pages;
															$scope.pageList = [];
															$scope.selPage = 1;
															$scope.setData = function() {
																$scope.items = $scope.DRs
																		.slice(
																				($scope.pageSize * ($scope.selPage - 1)),
																				($scope.selPage * $scope.pageSize));
															}
															$scope.items = $scope.DRs
																	.slice(
																			0,
																			$scope.pageSize);
															for (var i = 0; i < $scope.newPages; i++) {
																$scope.pageList
																		.push(i + 1);
															}
															$scope.selectPage = function(
																	page) {
																
																$rootScope.page= page;
																
																console.log($rootScope.page);
																if (page < 1
																		|| page > $scope.pages)
																	return;

																if(page==1){
																	var newPageList = [];
																	if($scope.pages>5){
																		for(var i = 1;i<=5; i++){
																			  newPageList.push(i);
																		}
																	}
																	else{
																		for(var i = 1;i<=$scope.pages; i++){
																		  newPageList.push(i);
																		}
																	}
																	
																	$scope.pageList = newPageList;
																}
																if (page > 2) {
																	var newpageList = [];
																	for (var i = (page - 3); i < ((page + 2) > $scope.pages ? $scope.pages
																			: (page + 2)); i++) {
																		newpageList
																				.push(i + 1);
																	}
																	$scope.pageList = newpageList;
																}
																$scope.selPage = page;
																$scope
																		.setData();
																$scope
																		.isActivePage(page);
															};

															$scope.isActivePage = function(
																	page) {
																return $scope.selPage == page;
															};
															$scope.Previous = function() {
																$scope
																		.selectPage($scope.selPage - 1);
															}
															$scope.Next = function() {
																$scope
																		.selectPage($scope.selPage + 1);
															};

														});

									})
										
									
										$scope.export =  function(){		
											$http({
												method :'POST',
												url: '/DRMS/api/DrmsDemandRequestPriority/export',
												headers: {
												'Content-Type': 'application/json'
												},
												data :$scope.DRs,
												responseType: 'arraybuffer'
												
											}).success(function (data) {
										      
												
												
												var blob = new Blob([data], {type: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"});
										   
												var objectUrl = URL.createObjectURL(blob);
										        var a = document.createElement('a');
										        document.body.appendChild(a);
										        a.setAttribute('style', 'display:none');
										        a.setAttribute('href', objectUrl);
										        a.setAttribute('download', "Demand Priority.xls");
										        a.click();
										        URL.revokeObjectURL(objectUrl);
										    });
									
									}
									
									
										$scope.dateStart = $scope.start;
										//console.log($scope.start);
										$scope.format = "MMM/dd/yyyy";
										$scope.altInputFormats = [ 'yyyy/M!/d!' ];
										$scope.popup1 = {
											opened : false
										};
										$scope.open1 = function() {
											$scope.popup1.opened = true;		
										}
										$scope.popup2 = {	
												opened : false
											};
										$scope.open2 = function() {
											$scope.popup2.opened = true;		
										};
									 $scope.option = function(DP, event){
								//	 console.log(DP	);	
									 $rootScope.demandPriority = DP;
									 //console.log($rootScope.demandRequest);
									 $state.go('recrut2.dmdpri.option');
									 event.stopPropagation();
									 event.preventDefault();
	}

				});
